import logging
from datetime import datetime
# logging.debug('调试')
# logging.info('信息')
# logging.warning('警告')
# logging.error('错误')
# logging.critical('严重错误')
class Logger:
    def get_logger(self,level,msg):
        self.logger=logging.getLogger('自动化测试日志')
        self.logger.setLevel('DEBUG') #从debug开始收集
        self.format=logging.Formatter('%(asctime)s --- %(name)s - %(message)s') #日志收集格式

        #设置控制台
        fh=logging.StreamHandler() #实例化控制台
        fh.setLevel('DEBUG')
        fh.setFormatter(self.format)

        #设置文件日志
        name=datetime.now().date()
        filename=('./test_report/log/'+str(name)+'.txt')
        sh=logging.FileHandler(filename,encoding='utf-8') #实例化日志
        sh.setLevel('DEBUG')
        sh.setFormatter(self.format)
        #绑定日志
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)

        if level == 'DEBUG':
            self.logger.debug(msg)
        elif level == 'INFO':
            self.logger.info(msg)
        elif level == 'WARNING':
            self.logger.warning(msg)
        elif level == 'ERROR':
            self.logger.error(msg)
        elif level == 'CRITICAL':
            self.logger.critical(msg)
        #解除绑定
        self.logger.removeHandler(fh)
        self.logger.removeHandler(sh)

    def debug(self,msg):
        self.get_logger('DEBUG',msg)
    def info(self,msg):
        self.get_logger('INFO',msg)
    def warning(self,msg):
        self.get_logger('WARNING',msg)
    def error(self,msg):
        self.get_logger('ERROR',msg)
    def critical(self,msg):
        self.get_logger('CRITICAL',msg)
